﻿@inject IStringLocalizer<Foo> LocalizerFoo
@inject ToastService ToastService

<Table TItem="Foo"
        IsPagination="true" PageItemsSource="@PageItemsSource"
        IsStriped="true" IsBordered="true" IsMultipleSelect="true"
        ShowToolbar="true" ShowDefaultButtons="false" ShowExportButton="true"
        OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" Width="100" />
        <TableColumn @bind-Field="@context.Address" />
        <TableColumn @bind-Field="@context.Count" />
    </TableColumns>
    <ExportButtonDropdownTemplate Context="ExportContext">
        <div class="dropdown-item" @onclick="ExportContext.ExportAsync">
            <i class="fa-regular fa-file-excel"></i>
            <span>MS-Excel</span>
        </div>
        <div class="dropdown-item" @onclick="@CsvExportAsync">
            <i class="fa-regular fa-file-excel"></i>
            <span>MS-CSV</span>
        </div>
    </ExportButtonDropdownTemplate>
</Table>

@code {
    /// <summary>
    /// Foo 类为Demo测试用，如有需要请自行下载源码查阅
    /// Foo class is used for Demo test, please download the source code if necessary
    /// https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/main/src/BootstrapBlazor.Shared/Data/Foo.cs
    /// </summary>
    [NotNull]
    private List<Foo>? Items { get; set; }

    private static IEnumerable<int> PageItemsSource => new int[] { 4, 10, 20 };
    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = Foo.GenerateFoo(LocalizerFoo);
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        IEnumerable<Foo> items = Items;

        // 设置记录总数
        var total = items.Count();

        // 内存分页
        items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total
        });
    }

    private Task CsvExportAsync()
    {
        //your code ...
        
        return ToastService.Success("CSV export", "Export CSV data successfully");
    }
}
